package com.hoperun.framework.jdbc.dialect;

public class MybatisOracleDialect extends Dialect {
	public boolean supportsLimit() {
		return true;
	}

	public boolean supportsLimitOffset() {
		return true;
	}

	public String getLimitString(String sql, int offset,
			String offsetPlaceholder, int limit, String limitPlaceholder) {
		int temp = 11;
		int tempInt = 100;
		String sqlstr = sql.trim();
		boolean isForUpdate = false;
		if (sqlstr.toLowerCase().endsWith(" for update")) {
			sqlstr = sqlstr.substring(0, sqlstr.length() - 11);
			isForUpdate = true;
		}

		StringBuffer pagingSelect = new StringBuffer(sqlstr.length() + 100);

		pagingSelect
				.append("select * from ( select row_.*, rownum rownum_ from ( ");

		pagingSelect.append(sqlstr);

		String offsetPlaceholderstr = offsetPlaceholder;
		if ((offsetPlaceholderstr != null)
				&& (!"".equals(offsetPlaceholderstr))) {
			Integer offsetTemp = Integer.valueOf(Integer
					.parseInt(offsetPlaceholderstr));

			offsetPlaceholderstr = String.valueOf(offsetTemp);
		}
		String endString = offsetPlaceholderstr + "+" + limitPlaceholder;
		pagingSelect.append(" ) row_ ) where rownum_ <= " + endString
				+ " and rownum_ > " + offsetPlaceholderstr);

		if (isForUpdate) {
			pagingSelect.append(" for update");
		}

		return pagingSelect.toString();
	}
}